

public class MinDepth {
    private static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val)
        {
            this.val = val;
        }
    }
    class Solution {
        public int minDepth(TreeNode root) {
            return getMin(root);
        }



        private int getMin(TreeNode root){
            if(root == null) return 0;
            int l1 = getMin(root.left);
            int r1 = getMin(root.right);
            if(root.left == null && root.right != null)
            {
                return 1 + r1;
            }
            if(root.left != null && root.right == null)
            {
                return 1 + l1;
            }
            return 1 + Math.min(l1,r1);


        }
    }
}
